import 'package:flutter/material.dart';
import 'package:flutter_baby_health_tracker/screens/all_record.dart';
import 'package:flutter_baby_health_tracker/screens/calendar.dart';
import 'package:flutter_baby_health_tracker/screens/leading.dart';
import 'package:flutter_baby_health_tracker/screens/records/Diaper.dart';
import 'package:flutter_baby_health_tracker/screens/records/Event.dart';
import 'package:flutter_baby_health_tracker/screens/records/Feed.dart';
import 'package:flutter_baby_health_tracker/screens/records/Growth.dart';
import 'package:flutter_baby_health_tracker/screens/records/Health.dart';
import 'package:flutter_baby_health_tracker/screens/records/Leisure.dart';
import 'package:flutter_baby_health_tracker/screens/records/Sleep.dart';
import 'package:flutter_baby_health_tracker/screens/setting.dart';
import 'package:flutter_baby_health_tracker/screens/statistic.dart';
import 'package:flutter_baby_health_tracker/util/sp_util.dart';
import 'package:flutter_baby_health_tracker/widgets/card.dart';
import 'package:get/get.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Future.wait([
    initializeDateFormatting(),
    SpUtil().init()
  ]).then((_){
    runApp(
      GetMaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          scaffoldBackgroundColor: Colors.grey.shade100,
          appBarTheme: AppBarTheme(
            backgroundColor: Colors.grey.shade100,
            foregroundColor: Colors.grey.shade600,
            shadowColor: Colors.white,
            surfaceTintColor: Colors.transparent,
            elevation: 1,
            scrolledUnderElevation: 0,
          ),
          focusColor: Colors.green,
          dialogTheme: DialogTheme(backgroundColor: Colors.white),
        ),
        themeMode: ThemeMode.light,
        home: firstRun() ?LeadingScreen(): App(),
      ),
    );
  });
}

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

bool firstRun()  {
  final r = SpUtil().get('first_run')??false;
  return r;
}

class _AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    final first = SpUtil().get('first_run');
    if(first==null||first==true){
      return LeadingScreen();
    }
    return Scaffold(
      body: Column(
        children: [
          SizedBox(
            height: kToolbarHeight,
          ),
          Container(
            padding: EdgeInsets.all(8),
            margin: EdgeInsets.all(8),
            child: Column(
              children: [
                Row(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: Colors.white,
                        border: Border.all(
                          color: Colors.green,width: 2
                        )
                      ),
                      padding: EdgeInsets.all(20),
                      child: Center(
                        child: Text("宝", style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,color: Colors.green)),
                      ),
                    ),
                    SizedBox(
                      width: 24,
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Jinney',style: TextStyle(
                          fontSize: 30
                        ),),
                        Text('3个月2天', style: TextStyle(fontSize: 14)),
                      ],
                    )
                  ],
                )
              ],
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              IconButton(
                onPressed: () {
                  Get.to(StatisticScreen());
                },
                icon: Icon(Icons.show_chart),
              ),
              IconButton(
                onPressed: () {
                  Get.to(CalendarScreen());
                },
                icon: Icon(Icons.calendar_today_rounded),
              ),
              IconButton(
                onPressed: () {
                  Get.to(SettingScreen());
                },
                icon: Icon(Icons.settings),
              ),
            ],
          ),
          Expanded(child: HomeView()),
        ],
      ),
    );
  }
}

class HomeView extends StatelessWidget {
  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {


    final vw = MediaQuery.of(context).size.width;
    final size = (vw - 8 * 6) / 5;
    return SingleChildScrollView(
      child: Column(
        children: [
          Divider(height: 1),
          Container(
            margin: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                spacing: 8,
                children: [
                  GestureDetector(
                    onTap: (){
                      Get.to(FeedScreen());
                    },
                    child: Container(
                      width: size,
                      height: size,
                      decoration: BoxDecoration(
                        color: Colors.amber,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Icon(
                          Icons.emoji_food_beverage,
                          color: Colors.white,
                          size: 32,
                        ),
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      Get.to(Sleep());
                    },
                    child: Container(
                      width: size,
                      height: size,
                      decoration: BoxDecoration(
                        color: Colors.blueAccent,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Icon(
                          Icons.bedroom_baby_rounded,
                          color: Colors.white,
                          size: 32,
                        ),
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      Get.to(Diaper());
                    },
                    child: Container(
                      width: size,
                      height: size,
                      decoration: BoxDecoration(
                        color: Colors.teal,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Icon(
                          Icons.baby_changing_station,
                          color: Colors.white,
                          size: 32,
                        ),
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      Get.to(Leisure());
                    },
                    child: Container(
                      width: size,
                      height: size,
                      decoration: BoxDecoration(
                        color: Colors.purple,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Icon(
                          Icons.emoji_emotions_outlined,
                          color: Colors.white,
                          size: 32,
                        ),
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      Get.to(Health());
                    },
                    child: Container(
                      width: size,
                      height: size,
                      decoration: BoxDecoration(
                        color: Colors.pink,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Icon(
                          Icons.medical_information,
                          color: Colors.white,
                          size: 32,
                        ),
                      ),
                    ),
                  ),

                  GestureDetector(
                    onTap: (){
                      Get.to(Growth());
                    },
                    child: Container(
                      width: size,
                      height: size,
                      decoration: BoxDecoration(
                        color: Colors.cyan,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Icon(Icons.trending_up, color: Colors.white, size: 32),
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      Get.to(Event());
                    },
                    child: Container(
                      width: size,
                      height: size,
                      decoration: BoxDecoration(
                        color: Colors.indigoAccent,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Icon(
                          Icons.emoji_events,
                          color: Colors.white,
                          size: 32,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Divider(height: 1),
          LiCard(child: Column(
            children: [
              Row(
                children: [
                  Text('进行中',style: TextStyle(
                    fontWeight: FontWeight.bold
                  ),),
                ],
              ),
              SizedBox(
                height: 16,
              ),
              Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(bottom: 16.0),
                    child: Row(
                      children: [
                        Container(
                          padding: EdgeInsets.all(8),
                          decoration: BoxDecoration(
                            color: Colors.amber,
                            shape: BoxShape.circle,
                          ),
                          child: Icon(Icons.local_drink, color: Colors.black54),
                        ),
                        SizedBox(
                          width: 16,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('休闲'),
                            Text('进行中')
                          ],
                        ),
                        Expanded(child: Container()),
                        Text('9:23',style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 24
                        )),
                        SizedBox(
                          width: 60,
                        ),
                        Icon(Icons.arrow_forward_ios,color: Colors.grey,)
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: Row(
                      children: [
                        Container(
                          padding: EdgeInsets.all(8),
                          decoration: BoxDecoration(
                            color: Colors.amber,
                            shape: BoxShape.circle,
                          ),
                          child: Icon(Icons.local_drink, color: Colors.black54),
                        ),
                        SizedBox(
                          width: 16,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('休闲'),
                            Text('进行中')
                          ],
                        ),
                        Expanded(child: Container()),
                        Text('9:23',style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 24
                        ),),
                        SizedBox(
                          width: 60,
                        ),
                        Icon(Icons.arrow_forward_ios,color: Colors.grey,)
                      ],
                    ),
                  ),
                ],
              )
            ],
          )),
          LiCard(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('最近活动',style: TextStyle(
                  fontWeight: FontWeight.bold
                ),),
                SizedBox(
                  height: 16,
                ),
                Column(
                  children: [
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                  ],
                ),
              ],
            ),
          ),
          LiCard(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('今日总结',style: TextStyle(
                  fontWeight: FontWeight.bold
                ),),
                SizedBox(
                  height: 16,
                ),
                Column(
                  children: [
                    ListTile(
                      title: Text('1次，30分钟'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                    ListTile(
                      title: Text('1分组前，30分钟，奶瓶'),
                      leading: Container(
                        padding: EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          shape: BoxShape.circle,
                        ),
                        child: Icon(Icons.local_drink, color: Colors.black54),
                      ),
                      dense: true,
                      contentPadding: EdgeInsets.zero,
                    ),
                  ],
                ),
              ],
            ),
          ),
          GestureDetector(
            onTap: () {
              Get.to(AllRecordScreen());
            },
            child: Container(
              decoration: BoxDecoration(
                color: Color(0xff2fd06f),
                borderRadius: BorderRadius.circular(8),
              ),
              margin: EdgeInsets.all(8),
              padding: EdgeInsets.symmetric(vertical: 16),
              child: Center(
                child: Text(
                  '所有记录',
                  style: TextStyle(color: Colors.grey.shade50, fontSize: 16),
                ),
              ),
            ),
          ),
          SizedBox(height: kToolbarHeight),
        ],
      ),
    );
  }
}
